﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STKMIS.BLL;
using STKMIS.Model;

namespace STKMIS
{
    public partial class FrmPurchaseSetSaleUnit : Form
    {
        public int productID=0;
        public int unitID=0;
        public string unitName="";
        public decimal purchaseAmout=0m;
        public decimal purchasePrice=0m;

        public List<ProductsAndUnitModel> productsAndUnitInfoList = new List<ProductsAndUnitModel>();
        public FrmPurchaseSetSaleUnit()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {  
            DialogResult = DialogResult.Yes;
        }

        private void FrmPurchaseSetSaleUnit_Load(object sender, EventArgs e)
        {
            this.lblPurchaseUnit.Text = unitName;
            DataTable dtUnit = ProductUnit.GetProductUnitList("");
            cmbUnit.DataSource = dtUnit;
            cmbUnit.DisplayMember = "ProUnitName";
            cmbUnit.ValueMember = "ProUnitID";
            lstbSaleUnit.DisplayMember = "Display";
            lstbSaleUnit.ValueMember = "Value";
            if (productsAndUnitInfoList != null && productsAndUnitInfoList.Count > 0)
            {
                foreach (ProductsAndUnitModel productAndUnitInfo in productsAndUnitInfoList)
                {
                    Item_valuetext item = new Item_valuetext(productAndUnitInfo.ProductUnitID.ToString(), productAndUnitInfo.PerAmount + "  " + productAndUnitInfo.ProductUnitName);
                    lstbSaleUnit.Items.Add(item);
                }
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtAmount.Text.Trim() != "")
            {
                if (cmbUnit.Text.Trim() == unitName)
                {
                    //不能选择主单位
                    MessageBox.Show("不能选择采购单位！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                //排除已选择单位
                foreach (ProductsAndUnitModel productAndUnitInfoTemp in productsAndUnitInfoList)
                {
                    if (productAndUnitInfoTemp.ProductUnitName == cmbUnit.Text.Trim())
                    {
                        MessageBox.Show("此单位已选择，请选择其他单位！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                Item_valuetext item = new Item_valuetext(cmbUnit.SelectedValue.ToString(), txtAmount.Text + "  " + cmbUnit.Text);
                lstbSaleUnit.Items.Add(item);
                ProductsAndUnitModel productAndUnitInfo = new ProductsAndUnitModel();
                productAndUnitInfo.ProductUnitID = Convert.ToInt32(cmbUnit.SelectedValue.ToString());
                productAndUnitInfo.PerAmount = Convert.ToDecimal(txtAmount.Text.ToString().Trim());
                productAndUnitInfo.ProductUnitName = cmbUnit.Text;
                productsAndUnitInfoList.Add(productAndUnitInfo);
                txtAmount.Text = "";
            }
            else
            {
                MessageBox.Show("请填写数量！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAmount.Focus();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstbSaleUnit.SelectedItems.Count > 0)
            {
                string unitID = ((Item_valuetext)lstbSaleUnit.SelectedItem).Value.Trim();
                lstbSaleUnit.Items.Remove(lstbSaleUnit.SelectedItems[0]);
                
                foreach (ProductsAndUnitModel productAndUnitInfo in productsAndUnitInfoList)
                {
                    if (productAndUnitInfo.ProductUnitID.ToString().Trim() == unitID)
                    {
                        productsAndUnitInfoList.Remove(productAndUnitInfo);
                        break;
                    }
                }
            }
        }

        private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            //检测是否已经输入了小数点
            bool IsContainsDot = txtAmount.Text.Contains(".");
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
            {
                e.Handled = true;
            }
            else if (IsContainsDot && (e.KeyChar == 46)) //如果输入了小数点，并且再次输入
            {
                e.Handled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
        }
    }

    public class Item_valuetext
    {

        private string display;

        private string truevalue; 

        public Item_valuetext(string name, string text)
        {

            this.display = text;

            this.truevalue = name;

        }

        public override string ToString()     //重载ToString()方法
        { 
            return this.display; 
        }



        public string Display
        { 
            get
            { 
                return this.display; 
            }

            set
            { 
                this.display = value; 
            } 
        }



        public string Value
        { 
            get
            { 
                return this.truevalue; 
            } 
            set
            { 
                this.truevalue = value; 
            } 
        }

    }

 
}
